import Vue from 'vue';
/**
 * 自定义指令 防止按钮被多次点击，重复请求
 * 用法：
 *  直接在元素上添加v-preventReClick
 **/
const preventReClick = Vue.directive('prc', {
  inserted: function (el, binding, vnode) {
    const { value } = binding; // arg,
    const time = 1000;
    let func = null; const that = vnode.context;
    el.addEventListener('click', event => {
      // console.log(arguments);
      if (!el.disabled) {
        el.disabled = true;
        if (func) {
          clearTimeout(func);
          event && event.stopImmediatePropagation();
          return;
        }
        func = setTimeout(() => {
          console.log(vnode);
          el.disabled = false;
          typeof value === 'function' ? value.apply(that, arguments) : null;
        }, time);
      }
    }, true);
  }
});
export default preventReClick;
